package com.woniuxy.handler;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.woniuxy.pojo.Guest;
import com.woniuxy.pojo.Member;
import com.woniuxy.pojo.Order_dgy;
import com.woniuxy.pojo.OrderDetail_dgy;
import com.woniuxy.pojo.RoomType_dgy;
import com.woniuxy.pojo.Room_dgy;
import com.woniuxy.service.DiscountService;
import com.woniuxy.service.GuestService;
import com.woniuxy.service.MemberService;
import com.woniuxy.service.OrderDetailService;
import com.woniuxy.service.OrderService;
import com.woniuxy.service.RoomService;
import com.woniuxy.service.RoomTypeService;

@Controller
@RequestMapping("/offline")
public class OfflineHandler{
	
	@Resource
	private MemberService memberService; 
	
	@Resource
	private DiscountService discountService;
	
	@Resource
	private GuestService guestService;
	
	@Resource
	private OrderService orderService;
	
	@Resource
	private OrderDetailService orderDetailService;
	
	@Resource
	private RoomService roomService;
	
	@Resource
	private RoomTypeService roomTypeService;
	
	@RequestMapping("/order")
	public String order(HttpServletRequest request){
		Order_dgy order=null;
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("order");
		if(obj!=null){
			order = (Order_dgy) obj;
		}
		//将订单添加到订单表中
		orderService.addOrder(order);
		//取订单id
		Order_dgy order1 = orderService.findByOrder(order);
		order.setOid(order1.getOid());
		List<OrderDetail_dgy> orderDetails = order.getOrderDetails();
		//订单项
		for(OrderDetail_dgy orderDetail : orderDetails){
			orderDetail.setOid(order1.getOid());
			orderDetail.setCheckin(order.getCheckIn());
			orderDetail.setCheckout(order.getCheckOut());
			//获取宾客信息
			Guest guest = orderDetail.getGuest();
			Guest guest1 = guestService.findGuest(guest);
			if(guest1==null){
				guestService.addGuest(guest);
				guest1 = guestService.findGuest(guest);
			}
			//获取gid
			orderDetail.setGid(guest1.getGid());
			//将订单项添加到表中
			orderDetailService.addOrderDetail(orderDetail);
		}
		order.setOpay("线下支付");
		order.setOstate("已消费");
		System.out.println(order+"------");
		orderService.changeOrder(order);
		return "html/offline.html";
	}
	
	@RequestMapping("/member")
	@ResponseBody
	public Order_dgy findByMnumner(String mnumber,HttpServletRequest request){
		Order_dgy order = null;
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("order");
		if(obj!=null){
			order = (Order_dgy) obj;
		}
		Member member = memberService.findByMnumber(mnumber);
		if(member==null){
			return null;
		}
		order.setMid(member.getMid());
		//查看全场折扣
		float percent = discountService.findDiscount();
		//查看会员打折情况
		float percentMember = discountService.findDiscountB(member.getMtype());
		//将打折后的总价格存到order中
		BigDecimal sumPrice = new BigDecimal(0);
		
		List<OrderDetail_dgy> orderDetails = order.getOrderDetails();
		
		for(OrderDetail_dgy orderDetail : orderDetails){
			BigDecimal newPrice = orderDetail.getOldprice().multiply(new BigDecimal(percent)).multiply(new BigDecimal(percentMember));
			orderDetail.setNewprice(newPrice);
			sumPrice=sumPrice.add(newPrice);
		}
		sumPrice=sumPrice.setScale(0, BigDecimal.ROUND_DOWN);
		order.setSumprice(sumPrice);
		if(member.getMtype().equals("白金卡会员")){
			order.setOpledge(new BigDecimal(0));
		}else{
			BigDecimal pledge = sumPrice.multiply(new BigDecimal(0.1)).setScale(0, BigDecimal.ROUND_DOWN);
			order.setOpledge(pledge);
		}
		order.setTotalprice(order.getOpledge().add(sumPrice));
		session.setAttribute("order", order);
		return order;
	}
	
	@RequestMapping("/cancle")
	public String cancle(HttpServletRequest request){
		Room_dgy room = new Room_dgy();
		HttpSession session = request.getSession();
		Order_dgy order =  (Order_dgy) session.getAttribute("order");
		List<OrderDetail_dgy> detail_dgies = order.getOrderDetails();
		for(OrderDetail_dgy detail_dgy : detail_dgies){
			room = roomService.findone(detail_dgy.getRid());
			room.setRstate("可入住");
			roomService.updateRoomState(room);
		}
		return "html/offline.html";
	}
	
	@RequestMapping("/car")
	public ModelAndView getCar(Order_dgy order,HttpServletRequest request) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		System.out.println(order);
		//订单生成时间
		Date date = new Date();
		order.setOstart(date);
		//生成订单号
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		String time = df.format(date);
		String orderNumber=time+ ((int)(Math.random()*8000+10000));
		order.setOnumber(orderNumber);
		Date date1 = sdf.parse(order.getCheckIn());
		Date date2 = sdf.parse(order.getCheckOut());
		int day = (int)(date2.getTime()-date1.getTime())/(24*60*60*1000);
		System.out.println(day);
		ModelAndView view = new ModelAndView();
		
		//获取订单项
		//获取session
		List<OrderDetail_dgy> detail_dgies = order.getOrderDetails();
		System.out.println(detail_dgies);
		BigDecimal sum = new BigDecimal("0");
		if(detail_dgies!=null){
			for(OrderDetail_dgy detail_dgy : detail_dgies){
				RoomType_dgy roomType = detail_dgy.getRoomType();
				detail_dgy.setDayamount(day);
				//通过房间类型查找房间信息 并分配房间
				List<Room_dgy> room = roomService.findByType1(roomType);
				detail_dgy.setRtid(room.get(0).getRtid());
				detail_dgy.setOldprice(roomType.getRprice().multiply(new BigDecimal(day)));
				//改变房间状
				room.get(0).setRstate("已入住");
				roomService.updateRoomState(room.get(0));
				//把该房间号传给用户
				detail_dgy.setRid(room.get(0).getRid());
				sum = sum.add(roomType.getRprice().multiply(new BigDecimal(day)));
			}
		}
		BigDecimal pledge = sum.multiply(new BigDecimal(0.1)).setScale(0, BigDecimal.ROUND_DOWN);
		order.setSumprice(sum);
		order.setOpledge(pledge);
		order.setTotalprice(sum.add(pledge));
		HttpSession session = request.getSession();
		session.setAttribute("order", order);
		view.addObject("order", order);
		view.setViewName("html/invoicing.html");
		return view;
	}
	
	@RequestMapping("/findRtype")
	@ResponseBody
	public List<RoomType_dgy> findRoom(String checkIn,String checkOut){
		Order_dgy dgy=new Order_dgy();
		dgy.setCheckIn(checkIn);
		dgy.setCheckOut(checkOut);
		List<RoomType_dgy> roomTypes = roomTypeService.findRoomType(dgy);
		return roomTypes; //write_article是视图名称（html文件名称）
	}

}
